# 题源类

import re
import jieba as jb
from .jieba_constants import *
from typing import Union


class SourceType:

    # 或者 jb.load_userdict(sourcedict.txt)
    # 具体使用方法可以参考官方文档。
    def __init__(self, src: str):
        self.src = src
        for word in TEST_WORDS:
            jb.add_word(word)
        self.src_list = list(filter(lambda x: x != " ", jb.cut(src)))

    def __str__(self):
        # 先判断必选的内容是否缺少,如果缺少就直接返回空字符串。
        if not (self.source_type and self.district):
            return ""
        # 年份
        strs = str(self.year)
        # 行政区划
        for key in self.district:
            if self.district[key]:
                strs += self.district[key]
        # 时间
        if self.date:
            strs += str(self.date) + "月"
        # 类型
        strs += self.source_type

        return strs

    @property
    def year(self) -> int:
        """获得年份,这里要求src必须是年份开头的字符串!!!"""
        return int(self.src_list[0])

    @property
    def district(self) -> dict:
        """获得行政区划，并作处理。"""
        district_dict = {"全国": None, "省": None, "市": None, "区": None, "县": None}
        grade = None
        for i in range(1, len(self.src_list)):
            if "全国" == self.src_list[i]:
                district_dict["全国"] = "全国"
                grade = "全国"
                break

            if "省" in self.src_list[i] and len(self.src_list[i]) >= 2:
                district_dict["省"] = self.src_list[i].replace("省", "")
                grade = "省"
            elif "市" == self.src_list[i][-1] and len(self.src_list[i]) > 2:
                district_dict["市"] = self.src_list[i].replace("市", "")
                grade = "市"
            elif "区" == self.src_list[i][-1]:
                if len(self.src_list[i]) > 1:
                    district_dict["区"] = self.src_list[i].replace("区", "")
                elif grade == "市" and len(self.src_list[i - 1]) > 1:
                    district_dict["区"] = self.src_list[i - 1]
                    grade = "区"
            elif "县" == self.src_list[i][-1]:
                if len(self.src_list[i]) > 1:
                    district_dict["县"] = self.src_list[i].replace("县", "")
                elif grade == "市" and len(self.src_list[i - 1]) > 1:
                    district_dict["县"] = self.src_list[i - 1]
                    grade = "县"
        if grade:
            return district_dict

    @property
    def date(self) -> Union[int, None]:
        """获得试题考试的时间（月份，可选）"""
        number_dict = NUMBER_DICT
        num = None
        for i in range(1, len(self.src_list)):
            if "月" == self.src_list[i][-1]:
                if len(self.src_list[i]) > 1:
                    # 出现“十二月”，“六月”的情况
                    try:
                        num = number_dict[self.src_list[i].replace("月", "")]
                        return num
                    except:
                        pass
                else:
                    # 前面是月份的情况。
                    try:
                        num = int(self.src_list[i - 1])
                        return num
                    except:
                        pass
        return num

    @property
    def source_type(self) -> str:
        type_str = ""
        old_words = OLD_PAPER
        new_words = NEW_PAPER
        option_words = OPTIONS_OLD
        other_important = TEST_TYPES
        china_numbers = NUMBER_ZH
        old = False
        for i in range(1, len(self.src_list)):
            if self.src_list[i] in old_words:
                # 判断是不是老高考卷
                type_str += self.src_list[i]
                old = True
                continue
            if old == True and (self.src_list[i] in option_words):
                # 判断分不分文理科
                type_str += self.src_list[i]
                return type_str
            if (self.src_list[i] in new_words) and self.src_list[-1] == "卷":
                # 如果是新高考
                type_str += "新高考"
                for j in range(1, len(self.src_list)):
                    if "卷" == self.src_list[j][-1]:
                        type_str += self.src_list[j - 1] + self.src_list[j]
                        return type_str
            if self.src_list[i] in other_important:
                # 如果是其他的重要考试
                if "模" == self.src_list[i]:
                    # 如果是模拟考
                    try:
                        num = int(self.src_list[i - 1])
                        type_str += china_numbers[num] + "模"
                        return type_str
                    except:
                        type_str += self.src_list[i - 1] + "模"
                        return type_str
                else:
                    type_str += self.src_list[i]

        if type_str != "":
            return type_str
